<?php defined('SYSPATH') or die('No direct access allowed.');
 
class Controller_Course extends Controller_Site {
    public function action_index() {
        $course = ORM::factory('course')->order_by('date')->find_all();
        $this->setVar('course_list', $course);
    }
    
    public function action_new() {        
        $form = View::factory('course/form');
        $ckeditor = new CKEditor();
        $config = array("toolbar" =>
        array(
                    array(
        'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink', '-', 'Table'
                    )
                )
                );
        
        $form->set('ckeditor', $ckeditor);
        $form->set('config', $config);
        $course = ORM::factory('course');
        $form->set('course', $course);
        if($this->request->method() == 'POST') {
            try {
                $course = ORM::factory('course');
                $form->set('course', $course);
                $this->processForm($course);
                $this->setFlash('notice', 'Sikeresen felvetted az új tanfolyamot!');
                $this->request->redirect('course');
            }
            catch (ORM_Validation_Exception $e) {
                $form->set('errors', $e->errors('models'));
            }
        }
                
        $this->content->set('form', $form);
    }
    
    protected function clearfilename($name){
	$mit = array('á','é','í','ö','ő','ó','ü','ű','ú','Ö','Ü','Ó','Ő','Ú','É','Á','Ű','×',' ');
	$mire = array('a','e','i','o','o','o','u','u','u','O','U','O','O','U','E','A','U','x','_');
	$newstr = str_replace($mit, $mire, $name);
	return $newstr;
    }
    
    protected function processForm($course){
        $course->name = $this->request->post('name');
        $course->description = $this->request->post('description');
        $course->date = $this->request->post('date');
        $course->from_time = $this->request->post('from_time');
        $course->to_time = $this->request->post('to_time');
        $course->place = $this->request->post('place');
        $course->save();

        if(isset($_FILES['files'])){
            for($i=0; $i<count($_FILES['files']['name']); $i++){
                $coursefile = ORM::factory('file');
                $_FILES['files']['name'][$i] = $this->clearfilename($_FILES['files']['name'][$i]);
                if($_FILES['files']['error'][$i] === UPLOAD_ERR_OK){
                    move_uploaded_file($_FILES['files']['tmp_name'][$i], realpath(Upload::$default_directory).DIRECTORY_SEPARATOR.'coursefiles'.DIRECTORY_SEPARATOR.$_FILES['files']['name'][$i]);
                    $coursefile->file = $_FILES['files']['name'][$i];
                    $coursefile->course_id = $course->id;
                    $coursefile->save();
                }
            }
        }
    }
    
     public function action_edit() {
        $param=$this->request->param('id');
        $form = View::factory('course/form')->set('action', URL::site('course/edit/'.$param));
        $ckeditor = new CKEditor();
        $config = array("toolbar" =>
        array(
                    array(
        'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink', '-', 'Table'
                    )
                )
                );
        
        $form->set('ckeditor', $ckeditor);
        $form->set('config', $config);
        $course = ORM::factory('course', $param);
        $files = ORM::factory('file')->where('course_id', '=', $param)->find_all();
        if($this->request->method() == 'POST') {
            try {
                $this->processForm($course);
                $this->setFlash('notice', 'Sikeresen módosítottad tanfolyamot!');
                $this->request->redirect(('/course'));
            }
            catch (ORM_Validation_Exception $e) {
                $form->set('errors', $e->errors('models'));
            }
        }
        $form->set('course', $course);
        $form->set('files', $files);
        $this->content->set('form', $form);
     }
     
     public function action_show(){
         $param=$this->request->param('id');
         $course = ORM::factory('course', $param);
         $files = ORM::factory('file')->where('course_id', '=', $param)->find_all();
         $script = new Facebook();
         $likebutton=$script->likebutton(URL::base('http', FALSE).'course/show/'.$param, false, 'standard', 450, false, 'like', 'light', 'arial');
         $this->content->set('course', $course)->set('files', $files)->set('likebutton', $likebutton);     
     }
     
     public function action_delete(){
        $param=$this->request->param('id');
        $course = ORM::factory('course', $param);
        $files = ORM::factory('file')->where('course_id', '=', $param)->find_all();
        foreach($files as $file){
            unlink(realpath(Upload::$default_directory).DIRECTORY_SEPARATOR.'coursefiles'.DIRECTORY_SEPARATOR.$file->file);
        }
        $course->delete();
        $this->setFlash('notice', 'Tanfolyam törölve!');
        $this->request->redirect('course');
    }
        
    public function action_deletefile(){
        $param = $this->request->param('id');   
        $file = ORM::factory('file', $param);
        $course_id=$file->course_id;
        unlink(realpath(Upload::$default_directory).DIRECTORY_SEPARATOR.'coursefiles'.DIRECTORY_SEPARATOR.$file->file);
        $file->delete();
        $this->request->redirect('course/edit/'.$course_id);
    }
}